home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / COMMON / udpmgr.c < prev    next >
C/C++ Source or Header  |  1979-12-31  |  5KB  |  245 lines

  1. /* --------------------------------- udpmgr.c ------------------------------- */
  2.  
  3. /* This is part of the flight simulator 'fly8'.
  4.  * Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  5. */
  6.  
  7. /* This client talks to the fly8udp server and can be used to shut it down
  8.  * with the 'shutdown' request.
  9.  * You can request 'stats' from the server too.
  10.  * To quit use 'end'.
  11. */
  12.  
  13. #include "config.h"
  14.  
  15. #ifdef HAVE_UDP
  16.  
  17. #include <stdio.h>
  18. #define PHEAD        msg
  19. #include "fly8udp.h"
  20.  
  21.  
  22. static char    admin[LADDRESS] = ADMIN_ADDR;
  23. static char    *pname = 0;
  24.  
  25. #ifdef MSDOS
  26. static char    FAR inbuf[1500] = "", *pbuf = inbuf;
  27. #endif
  28.  
  29. static char    FAR msg[1500];            /* message buffer */
  30.  
  31. int
  32. main (int argc, char *argv[])
  33. {
  34.     int            fd;
  35.     struct sockaddr_in    svr, cli;
  36.     char            *protoname;
  37.     struct protoent        *proto;
  38.     long            nin;        /* incomming msgs counter */
  39.     int            n;        /* incomming msg length */
  40.     unsigned long        srv_addr;    /* server address */
  41.     struct hostent        *hostptr;
  42.     int            len;
  43.     char            *servname;
  44.  
  45.     pname = argv[0];
  46.  
  47.     if (argc < 2)
  48.         servname = "localhost";
  49.     else
  50.         servname = argv[1];
  51.  
  52. #ifdef WATTCP
  53.     sock_init ();
  54.  
  55.     if ((fd = socket (AF_INET, SOCK_FLY8, IPPROTO_UDP)) < 0) {
  56. #else
  57.     protoname = "udp";
  58.     if ((proto = getprotobyname (protoname)) == NULL) {
  59.         printf ("%s: getprotobyname(%s) failed: %s\n", 
  60.             pname, protoname, strerror (errno));
  61.         exit (1);
  62.     }
  63.  
  64.     if ((fd = socket (AF_INET, SOCK_FLY8, proto->p_proto)) < 0) {
  65. #endif
  66.         printf ("%s: socket() failed: %s\n", pname, strerror (errno));
  67.         exit (1);
  68.     }
  69.  
  70. #ifndef MSDOS
  71.     n = fcntl (fd, F_GETFL);
  72.     if (fcntl (fd, F_SETFL, n|FLY8_NONBLOCK) < 0) {
  73.         printf ("%s: fcntl(socket) failed: %s\n",
  74.             pname, strerror (errno));
  75.         exit (1);
  76.     }
  77. #endif
  78.  
  79. /* Set up server (our output) address.
  80. */
  81.     if ((hostptr = gethostbyname (servname)) == NULL) {
  82.         printf ("%s: gethostbyname(%s) failed: %s\n", 
  83.             pname, servname, strerror (errno));
  84.         exit (1);
  85.     }
  86.  
  87.     if (hostptr->h_addrtype != AF_INET) {
  88.         printf ("%s: not AF_INET address\n", 
  89.             pname);
  90.         exit (1);
  91.     }
  92.  
  93.     srv_addr = ((struct in_addr *)hostptr->h_addr_list[0])->s_addr;
  94.  
  95.     memset (&svr, 0, sizeof (svr));
  96.     svr.sin_family      = AF_INET;
  97.     svr.sin_addr.s_addr = srv_addr;
  98.     svr.sin_port        = htons (IPPORT_FLY8);
  99.  
  100.     printf ("server address is %08lx:%04x\n", 
  101.         ntohl (svr.sin_addr.s_addr), ntohs (svr.sin_port));
  102.     fflush (stdout);
  103.  
  104. /* Set up client (our input) address.
  105. */
  106.     memset (&cli, 0, sizeof (cli));
  107.     cli.sin_family      = AF_INET;
  108.     cli.sin_addr.s_addr = htonl (INADDR_ANY);
  109.     cli.sin_port        = htons (0);
  110.  
  111.     if (bind (fd, (struct sockaddr *) &cli, sizeof (cli)) < 0) {
  112.         printf ("%s: bind() failed: %s\n", pname, strerror (errno));
  113.         exit (1);
  114.     }
  115.  
  116.     printf ("my     address is %08lx:%04x\n", 
  117.         ntohl (cli.sin_addr.s_addr), ntohs (cli.sin_port));
  118.     fflush (stdout);
  119.  
  120. /* Set up stdin for non-blocking.
  121. */
  122. #ifndef MSDOS
  123.     n = fcntl (STDIN_FILENO, F_GETFL);
  124.     if (fcntl (STDIN_FILENO, F_SETFL, n|FLY8_NONBLOCK) < 0) {
  125.         printf ("%s: fcntl(stdin) failed: %s\n", pname, strerror (errno));
  126.         exit (1);
  127.     }
  128. #endif
  129.  
  130.     for (nin = 0;;) {
  131.  
  132. /* Accept replies from server.
  133. */
  134.         len = sizeof (cli);
  135.         n = recvfrom (fd, msg, sizeof (msg), 0,
  136.             (struct sockaddr *)&cli, (int *)&len);
  137.         if (n < 0) {
  138. #ifndef WATTCP
  139.             if (EWOULDBLOCK != errno) {
  140.                 printf ("%s: recvfrom() failed: %s\n", 
  141.                     pname, strerror (errno));
  142.                 break;
  143.             }
  144. #endif
  145. #ifdef WATTCP
  146.         } else if (n > 0) {
  147. #else
  148.         } else {
  149. #endif
  150.             printf ("%s %3u bytes from server: \"%s\"\n",
  151.                 pname, n, msg+PHSIZE);
  152.             fflush (stdout);
  153.         }
  154.  
  155. /* Get user input.
  156. */
  157. #ifdef MSDOS
  158.         n = 0;
  159.         while (kbhit ()) {
  160.             n = getche ();
  161.             if ('\r' == n)
  162.                 break;
  163.             *pbuf++ = (char)n;
  164.         }
  165.         if ('\r' != n)
  166.             continue;
  167.         putch ('\n');
  168.         *pbuf++ = '\n';
  169.         *pbuf++ = '\0';
  170.         strncpy (PHDATA, inbuf, sizeof(msg)-PHSIZE);
  171.         pbuf = inbuf;
  172. #else
  173.         if (NULL == fgets (PHDATA, sizeof(msg)-PHSIZE, stdin)) {
  174.             if (EWOULDBLOCK == errno)
  175.                 continue;
  176.             break;
  177.         }
  178. #endif
  179.  
  180. /* Check for quit message. The FLY8_NONBLOCK seems to make it impossible to
  181.  * get a good EOF on a ^D.
  182. */
  183.         if (!strcmp (PHDATA, "end\n") ||
  184.             !strcmp (PHDATA, "exit\n") ||
  185.             !strcmp (PHDATA, "quit\n"))
  186.             break;
  187.  
  188. /* Identify message as admin type.
  189. */
  190.         memset (msg, 0, PHSIZE);
  191.         memcpy (PHFROM, admin, LADDRESS);
  192.         n = strlen (PHDATA) + 1;
  193.         PHLEN[0] = (unsigned char)(0x0ff & (len >> 8));
  194.         PHLEN[1] = (unsigned char)(0x0ff & (len     ));
  195.  
  196.         n += PHSIZE;
  197.         if (n != sendto (fd, msg, n, 0, (struct sockaddr *)&svr,
  198.                             sizeof (svr))) {
  199. #ifndef WATTCP
  200.             if (EWOULDBLOCK == errno) {
  201.                 printf ("%s: WOULDBLOCK\n", 
  202.                     pname);
  203.                 fflush (stdout);
  204.                 continue;
  205.             }
  206. #endif
  207.             printf ("%s: sendto() failed: %s\n", 
  208.                 pname, strerror (errno));
  209.             break;
  210.         }
  211.         printf ("%s %ld: %3u bytes sent\n", 
  212.             pname, ++nin, n);
  213.         fflush (stdout);
  214.     }
  215.  
  216.     fflush (stdout);
  217.  
  218. #ifdef WATTCP
  219.     n_close (fd);
  220. #else
  221.     close (fd);
  222. #endif
  223.  
  224. /* Restore stdin for blocking.
  225. */
  226. #ifndef MSDOS
  227.     n = fcntl (STDIN_FILENO, F_GETFL);
  228.     fcntl (STDIN_FILENO, F_SETFL, n&~FLY8_NONBLOCK);
  229. #endif
  230.  
  231.     exit (0);
  232.     return (0);
  233. }
  234.  
  235. #else
  236.  
  237. int
  238. main (int argc, char *argv[])
  239. {
  240.     printf ("upd not available\n");
  241.     exit (0);
  242. }
  243.  
  244. #endif
  245.